package util.src.com;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @Author: Gosin
 * @Date: 2020/11/2 15:27
 */
public class GetErrorTsUrls extends WriteText {
    public String m3u8Path = "C:\\Users\\zz\\Desktop\\乱七八糟\\小工具\\N_m3u8DL-CLI\\start_GUI\\Downloads\\1\\";
    public String urlHeader = "https://ifeng.com-y-baidu.com/20190117/11296_6eaf75f5/1000k/hls/";
    public String tsPath = m3u8Path + "1\\";
    List<String> fileNames;
    public static Queue<String> queue = new LinkedList<String>();
    Collection succeedFileNames = new ArrayList();
    private int minTsLength;


    public GetErrorTsUrls(String allUrlTextPath, String allUrlTextName, String errorUrlTextPath, String errorUrlTextName,String urlHeader, String tsPath, int minTsLength) {
        super(allUrlTextPath, allUrlTextName, errorUrlTextPath, errorUrlTextName);
        this.urlHeader = urlHeader;
        this.tsPath = tsPath;
        File f = new File(tsPath);
        fileNames = Arrays.asList(f.list());
        this.minTsLength = minTsLength;
        for (String tsName : fileNames) {
            File ts = new File(tsPath + tsName);
            if (ts.length() > minTsLength) {
                succeedFileNames.add(tsName);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        String allUrlTextPath = "D:\\乱七八糟\\Downloads\\2\\";
        String allUrlTextName = "a.txt";
        String errorUrlTextPath = allUrlTextPath;
        String errorUrlTextName = "e.txt";
        String tsPath = allUrlTextPath + "1\\";
        int minTsLength = 1024 *1;
        GetErrorTsUrls a = new GetErrorTsUrls(allUrlTextPath, allUrlTextName, errorUrlTextPath, errorUrlTextName, "", tsPath, minTsLength);
        a.getTsUrlText();
    }

    @Override
    protected String selectUrl(String line) {
        //挑选时还要保证顺序不能错
        if (line != null) {
            //使用readLine方法，一次读一行
            if (!succeedFileNames.isEmpty()) {
                if (succeedFileNames.contains(line.split("/")[line.split("/").length - 1])) {
                    return null;
                }
                queue.offer(urlHeader + line);
                return line;
            }
            //添加元素
        }
        return null;
    }
}
